var vue = new Vue({
    el: '#app',
    data: {
        dialogRoleMenuVisible: false,
        loading: false,
        tableData: [],
        // saveOrUpdate: true,
        menuTreeData: [],
        checkTree: [],
        paramData: {
            authorized: true,
            roleName: '',
            currentPage: 1, //当前页数
            pageSize: 10, //每页条数
            total: 0 //总条数
        },
        treeProps: {
            children: 'menuChildren',
            label: 'name',
            disabled: 'disabled'
        },
        statusDic: {'0': '无效', '1': '有效'},
        roleTypeDic: {'ORG_PUB_R': '内置机构', 'ENT_PUB_R': '外置机构', 'SYS_R': '内置企业', 'FLOW_R': '外置企业'},
        dialogFormVisible: false,
        formLabelWidth: '350px',
        labelPosition: 'right',
        sysRoleVO: {
            id: '',
            menuIds: []
        },
        roleForm: {
            id: '',
            code: '',
            name: '',
            type: '',
            /*pubFlag: '0',
            moduleUrl: '',
            moduleImg: '',
            moduleDescr: '',*/
            remark: ''
        },
        roleFormRules: {
            roleCode: [
                {required: true, message: '请填写角色编码'}
            ],
            roleName: [
                {required: true, message: '请填写角色名称'},
            ],
            roleType: [
                {required: true, message: '请选择角色类型'},
            ],
        },
        dialogTitle: '新增角色',
        formRead: false
    },
    created: function () {
        this.initTable();
        this.initTree();
    },
    methods: {
        getStatusClass: getStatusIcon,
        handleSizeChange: function (pageSize) {
            this.paramData.pageSize = pageSize;
            this.initTable();
        },
        handleCurrentChange: function (pageCurrent) {
            this.paramData.currentPage = pageCurrent;
            this.initTable();
        },
        handleDialogClose: function () {
            this.$nextTick(function () {
                this.$refs['roleForm'].resetFields();
            }.bind(this))
        },
        onSearch: function () {
            var self = this;
            var data = {
                "name": this.paramData.roleName,
                "currentPage": this.paramData.currentPage,
                "pageSize": this.paramData.pageSize
            }
            Util.get('/sysRole/queryByName', data, function (result) {
                console.log(result.success)
                if (result.success) {
                    self.tableData = result.obj.records;
                    self.paramData.total = result.obj.total;

                }

            })
            /*this.paramData.current = 1;
            this.initTable();*/
        },
        addRole: function () {
            this.dialogTitle = '新增角色';
            this.formRead = false;
            // this.saveOrUpdate = true,
            this.roleForm = {
                id: '',
                code: '',
                name: '',
                type: '',
                /*pubFlag: '0',
                moduleUrl: '',
                moduleImg: '',
                moduleDescr: '',*/
                remark: ''
            };
            this.dialogFormVisible = true;
        },
        openDetail: function (row) {
            this.dialogTitle = '查看角色';
            $.extend(this.roleForm, row);
            this.formRead = true;
            this.dialogFormVisible = true;
            this.$nextTick(function () {
                this.$refs['roleForm'].validate();
            }.bind(this))
        },
        editDetail: function (row) {
            /*this.saveOrUpdate = false;*/
            this.dialogTitle = '编辑角色';
            $.extend(this.roleForm, row);
            this.roleForm.id = row.id;
            this.formRead = false;
            this.dialogFormVisible = true;
            this.$nextTick(function () {
                this.$refs['roleForm'].validate();
            }.bind(this))
        },
        /*滑块开关*/
        turnOnOff: function (row) {
            var self = this;
            row.status = row.status === 1 ? 0 : 1;
            var sysRole = row;
            Util.postJson('/sysRole/update', row, function (result) {
                if (result.success) {
                    self.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                    /*self.dialogFormVisible = false;*/
                    self.initTable();
                } else {
                    self.$message.error(result.msg)
                }
            })
        },
        /*// 新增或新增提交
        * saveOrUpdate=true时是新增
        * saveOrUpdate=false是编辑*/
        save: function () {
            if (this.roleForm.id !== '') {
                var self = this;
                self.$refs['roleForm'].validate(function (valid) {
                    if (valid) {
                        Util.postJson('/sysRole/update', self.roleForm, function (result) {
                            if (result.success) {
                                self.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                                self.dialogFormVisible = false;
                                self.initTable();
                            } else {
                                self.$message.error(result.msg)
                            }
                        })
                    } else {
                        return false;
                    }
                });

            } else {
                var self = this;
                self.$refs['roleForm'].validate(function (valid) {
                    if (valid) {
                        Util.postJson('/sysRole/save', self.roleForm, function (result) {
                            if (result.success) {
                                self.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                                self.dialogFormVisible = false;
                                self.initTable();
                            } else {
                                self.$message.error(result.msg)
                            }
                        })
                    } else {
                        return false;
                    }
                    this.roleForm.id = ''
                });

            }

        },
        /*删除*/
        deleteRow: function (m) {
            var self = this;
            self.$confirm('确定删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                Util.post('/sysRole/delete/' + m.id,
                    {id: m.id, code: m.code}, function (result) {
                        if (result.success) {
                            self.$message({
                                type: 'success',
                                message: '角色删除成功!'
                            });
                            self.initTable();
                        } else {
                            self.$message.error(result.msg);
                        }
                    });
            }).catch(function () {
                self.$message({
                    type: 'info',
                    message: '取消删除!'
                });
            });
        },
        //查看权限
        openRoleMenu: function (row) {

            this.dialogRoleMenuVisible = true;
            this.selectRow = row;
            var me = this;

            //树控件数据回显
            Util.post('/sysRole/roleMenu', {"id": this.selectRow.id}, function (result) {
                if (result.success) {
                    console.log(result.data)
                    //console.log("success")
                    let arr2 = result.data.map(v => {
                        return v.id
                    })
                    me.checkTree = arr2
                    //console.log(me.checkTree)
                }
            });

        },
        addDialogClosed() {
            //重置模型
            this.$refs.tree.setCheckedKeys([])
        },
        //初始化树
        initTree() {
            var me = this;
            //Util.postEncrypt('/system/menu/listAllMenuTree', null, function (result) {
            Util.postJson('/sysMenu/listMenuTree', null, function (result) {
                me.menuTreeData = result.data;
                console.log(result.data)
                if (result.success) {
                    me.menuTreeData = result.data;
                    //console.log("success")

                }
            });
        },
        savePermission() {
            console.log(this.selectRow)
            this.selectRow.id;
            var self = this;
            if (!self.selectRow) {
                this.$message.error("请选择需要授权的用户!");
                return;
            }
            this.checkTree = this.$refs.tree.getCheckedKeys();

            console.log(this.checkTree)

            this.sysRoleVO.id = this.selectRow.id;
            this.sysRoleVO.menuIds = this.checkTree;
            Util.postJson('/sysRole/updateRoleMenus', this.sysRoleVO, function (result) {

                //console.log("success")
                if (result.success) {
                    this.$message.success("授权成功!");
                } else {
                    this.$message.error("授权失败!");
                }

            });
            this.dialogRoleMenuVisible = false;
        },

        initTable: function () {
            var self = this;
            Util.get('/sysRole', self.paramData, function (result) {
                if (result.success) {
                    self.tableData = result.obj.records;
                    self.paramData.total = result.obj.total;
                }
            })
        },
        statusRender: function (value) {
            return this.statusDic[value];
        },
        roleTypeRender: function (value) {
            return this.roleTypeDic[value];
        }
    }
});